var gulp = require("gulp");
var gutil = require("gulp-util");

var typescript = require("gulp-tsc");
var uglify = require("gulp-uglify");
var concat = require("gulp-concat");
var clean = require("gulp-clean");
var rename = require("gulp-rename");

gulp.task("default", ["compress", "resources"], function () {
});

gulp.task("clean", function () {
    return gulp.src(["target"])
        .pipe(clean());
});

gulp.task("compile", function () {
    return gulp.src(["src/main/typescript/**/*.ts"])
        .pipe(typescript())
        .pipe(gulp.dest("target/javascript"));
});

gulp.task("concat", ["compile"], function () {
    var sources = [
        "target/javascript/**/*.js",
        "src/main/javascript/**/UI.*.js",
        "src/main/javascript/Data.js",
        "src/main/javascript/index.js",
        "src/main/javascript/draw-players.js",
        "src/main/javascript/draw-cities.js",
        "src/main/javascript/draw-tracks.js",
        "src/main/javascript/claim-track.js",
        "src/main/javascript/**/*.js"
    ];

    return gulp.src(sources)
        .pipe(concat("ttr-scorer-1.0.0.js"))
        .pipe(gulp.dest("target"));
});

gulp.task("compress", ["concat"], function () {
    return gulp.src(["target/ttr-scorer-1.0.0.js"])
        .pipe(uglify())
        .pipe(rename("ttr-scorer-1.0.0.min.js"))
        .pipe(gulp.dest("target"));
});

gulp.task("resources", function () {
    gulp.src(["src/main/resources/css/**/*.css"])
        .pipe(concat("ttr-scorer-1.0.0.css"))
        .pipe(gulp.dest("target"));

    gulp.src(["src/main/resources/**/*", "!src/main/resources/css{,/**/*.css}"])
        .pipe(gulp.dest("target"));
});

gulp.task("watch", function () {
    gulp.watch("src/main/typescript/**/*.ts", ["compress"]);
    gulp.watch("src/main/javascript/**/*.js", ["compress"]);
    gulp.watch("src/main/resources/**/*", ["resources"]);
});
